package com.example.fresh_market.adapter;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.fresh_market.R;
import com.example.fresh_market.enmus.OrderStatus;
import com.example.fresh_market.enmus.RefundStatus;
import com.example.fresh_market.entity.Order;

import java.util.ArrayList;
import java.util.List;

public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.BillViewHolder> {
    private final ArrayList<Order> mList;
    private final OnItemClickListener mListener;

    public OrderAdapter(ArrayList<Order> list, OnItemClickListener listener) {
        this.mList = list;
        this.mListener = listener;
    }


    @NonNull
    @Override
    public BillViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new OrderAdapter.BillViewHolder(
                LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.order_list_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(@NonNull BillViewHolder holder, int position) {
        Order target = getItem(position);
        holder.bindBean(target);
        holder.takeOrder.setOnClickListener(v -> {
            mListener.onTakeClick(v, holder.getAdapterPosition());
        });
        holder.refundOrder.setOnClickListener(v -> {
            mListener.onRefundClick(v, holder.getAdapterPosition());
        });
    }

    public Order getItem(int position) {
        return mList.get(position);
    }


    @Override
    public int getItemCount() {
        return mList.size();
    }

    public void setData(List<Order> list) {
        mList.clear();
        mList.addAll(list);
        notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        void onRefundClick(View view, int position);

        void onTakeClick(View view, int position);
    }

    public static class BillViewHolder extends RecyclerView.ViewHolder {
        private final TextView orderId;
        private final TextView createTime;
        private final TextView payType;
        private final TextView payPrice;
        private final TextView payStatus;
        private final TextView prodNum;
        private final Button refundOrder;
        private final Button takeOrder;

        BillViewHolder(View itemView) {
            super(itemView);
            orderId = itemView.findViewById(R.id.order_orderId);
            createTime = itemView.findViewById(R.id.order_createTime);
            payType = itemView.findViewById(R.id.order_payType);
            payPrice = itemView.findViewById(R.id.order_payPrice);
            payStatus = itemView.findViewById(R.id.order_payStatus);
            prodNum = itemView.findViewById(R.id.order_prodNum);
            refundOrder = itemView.findViewById(R.id.refund_order);
            takeOrder = itemView.findViewById(R.id.take_order);
        }

        protected void bindBean(final Order order) {
            orderId.setText(String.format("订单编号：%s", order.getId()));
            createTime.setText(String.format("下单时间：%s", order.getCreateTime()));
            payType.setText(String.format("支付方式：%s", "wechat".equals(order.getPayType()) ? "微信支付" : "其他方式"));
            payPrice.setText(String.format("总价：%s元", order.getPayPrice()));
            prodNum.setText(String.format("商品数量：%s", order.getTotalNum()));
            payStatus.setText(order.getPaid() > 0 ? "已支付" : "未支付");
            if (order.getRefundStatus() != RefundStatus.NOT_REFUND.getCode()) {
                payStatus.setText(RefundStatus.getByCode(order.getRefundStatus()).getDesc());
            } else {
                payStatus.setText(OrderStatus.getByCode(order.getStatus()).getDesc());
            }
            if (order.getStatus() != OrderStatus.WAITING_TO_RECEIVE.getCode()) {
                takeOrder.setVisibility(View.INVISIBLE);
            }
            if (order.getRefundStatus() != RefundStatus.NOT_REFUND.getCode()) {
                refundOrder.setVisibility(View.INVISIBLE);
            }
        }
    }
}
